<?php

namespace App\Http\Controllers\Admin;

use App\Jobs\Page\Query as PageQuery;
use App\Models\Page;
use App\Transformers\Page\PagesTransformer;
use App\Transformers\Page\PageTransformer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $pages = $this->dispatchNow(new PageQuery($request->all(), true, self::PAGE_SIZE));

        return $this->response->paginator($pages, new PagesTransformer(true));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $page = Page::with('user', 'category.parent')->find($id);

        return $this->response->item($page, new PageTransformer());
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $page = Page::find($id);
        if (!$page) {
            return $this->response->error('文章不存在', 422);
        }

        $page->delete();

        return $this->response->noContent();
    }
}
